package com.xuecheng.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.ucenter.mapper.XcUserMapper;
import com.xuecheng.ucenter.mapper.XcUserRoleMapper;
import com.xuecheng.ucenter.model.dto.RegisterDto;
import com.xuecheng.ucenter.model.po.XcUser;
import com.xuecheng.ucenter.model.po.XcUserRole;
import com.xuecheng.ucenter.service.RegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class RegisterServiceImpl implements RegisterService {
    @Autowired(required = false)
    private StringRedisTemplate stringRedisTemplate;
    @Autowired(required = false)
    private XcUserMapper xcUserMapper;
    @Autowired(required = false)
    private XcUserRoleMapper xcUserRoleMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public void registerInfo(RegisterDto registerDto) {
        String nickname = registerDto.getNickname();
        String username = registerDto.getUsername();

        //校验验证码，如果不一致则抛出异常
        String code = stringRedisTemplate.opsForValue().get(registerDto.getCellphone());
        if((!registerDto.getCheckcode().equals(code))){
            XueChengPlusException.cast("验证码错误！");
        }
        //从redis中删除验证码
        stringRedisTemplate.delete(registerDto.getCellphone());

        //校验两次密码是否一致，如果不一致则抛出异常
        String password = registerDto.getPassword();
        String confirmpwd = registerDto.getConfirmpwd();
        if((!password.equals(confirmpwd))){
            XueChengPlusException.cast("两次密码不一致！");
        }

        //校验用户是否存在，如果存在则抛出异常
        LambdaQueryWrapper<XcUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(XcUser::getUsername,username);
        XcUser xcUser = xcUserMapper.selectOne(wrapper);
        if(xcUser!=null){
            XueChengPlusException.cast("用户已存在！");
        }
        password=passwordEncoder.encode(password);
        xcUser=new XcUser();
        //向用户表、用户角色关系表添加数据。角色为学生角色。
        xcUser.setUsername(username);
        xcUser.setPassword(password);
        xcUser.setName(username);
        xcUser.setNickname(nickname);
        xcUser.setCompanyId("1232141425");
        xcUser.setUtype("101001");
        xcUser.setEmail(registerDto.getEmail());
        xcUser.setCellphone(registerDto.getCellphone());
        xcUser.setStatus("1");
        xcUser.setCreateTime(LocalDateTime.now());
        xcUser.setUpdateTime(LocalDateTime.now());
        int insert = xcUserMapper.insert(xcUser);
        if(insert<=0){
            XueChengPlusException.cast("添加用户信息失败！");
        }

        XcUserRole xcUserRole = new XcUserRole();
        xcUserRole.setUserId(xcUser.getId());
        xcUserRole.setRoleId("17");
        xcUserRole.setCreateTime(LocalDateTime.now());
        xcUserRoleMapper.insert(xcUserRole);
    }
}
